/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
// Myriad.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Myriad.h"

#include <stdio.h>
#include <time.h>

Kernel* g_kernel = NULL;


/**
===========================
Init each subsystem and add them to
the engine
===========================
*/
void SetupEngine()
{
    Console* console   = g_kernel->GetConsole();
    Cvars*   cvars     = g_kernel->GetCvars();
         
    console->Print( (_PROGRAM_ + _VERSION_ + "-- "+ _OS_ + "-- "+ __DATE__+ " " + " " __TIME__ ).c_str() ); 

    // we need to register the 'bind' cmds, so initialize the input system
    IInput* inputManager = new Input();
    inputManager->Init();
    g_kernel->SetInput( inputManager );

    // Read in the Config file
    console->Print("Loading config.cfg file...");
    console->Exec("exec config.cfg");
    console->SetLog ( cvars->GetFloatValue("logger") > 0 );
    
    console->Print("Constructing Subsystems...");
    IResourceManagement* resourceManager = new Resource();
    resourceManager->Init();
    g_kernel->SetResourceManagement( resourceManager );
    /*-----------------------------------------------
        Check for input type
    ------------------------------------------------*/
    
    // Register the console commandline buffer 
    inputManager->AddBuffer( console->GetBuffer(), BUFFER_CONSOLE );
    inputManager->ActivateBuffer( BUFFER_CONSOLE );
    inputManager->SetPriority( HIGH_PRIORITY );
   
    if ( cvars->GetFloatValue("i_joystick") )
        inputManager->EnableJoystick();


    console->Print("Initializing scripting engine...");
    IScriptEngine* scriptEngine = new LuaEngine();
    if ( scriptEngine->Init() )
        console->Print( ("***Error loading scripting engine -> "+scriptEngine->GetImplementation()).c_str() );
    else {
        console->Print("Scripting Engine Initialized!");
        console->Print( ("---- " + scriptEngine->GetImplementation() + " ----").c_str() );
        
        g_kernel->SetScriptEngine( scriptEngine );
    }


    // Add the sound subsystem
	ISound* soundManager = new SoundManager;
	soundManager->Init();
	soundManager->SetPriority( MED_PRIORITY );
    g_kernel->SetSound( soundManager );
    
	console->Print( "Sound System online..." );

    // Game 
    GameManager* gameManager = new GameManager();
    gameManager->Init();
    gameManager->SetPriority( HIGH_PRIORITY+1 );
    g_kernel->SetGame( gameManager );  

    console->Print("GameManager online!");

    // Make Renderer the least priority
    IRenderManager* renderManager = new RenderManager();
    renderManager->Init();
    renderManager->SetPriority( LOW_PRIORITY );    
    g_kernel->SetRenderer( renderManager );

    console->Print("Renderer online!");

    // Create the console view
    IGameStateView* consoleView = new ConsoleView(console);
    consoleView->SetZOrder( ZTOP_MOST+20 );
    consoleView->SetActive( true );
    // Add the console to the game
    gameManager->AddState( console, consoleView );
    
    /*----------------------------------------------------
        Add the Main Menu State
    ------------------------------------------------------*/
    IGameState* menuState = new MenuState;
    menuState->Init();
    gameManager->AddState( menuState, NULL );

    /*----------------------------------------------------
        Add the In Game State
    -----------------------------------------------------*/
    IGameState* inGameState = new InGameState;
    inGameState->SetActive( true );
    IGameStateView* inGameView = new InGameStateView;
    // Add the InGameState to the game
    inGameView->SetZOrder( ZBOTTOM_MOST );
    gameManager->AddState( inGameState, inGameView );
    
    /*----------------------------------------------------
        Add the Dialog state
    -----------------------------------------------------*/
    // dialog box -- TEMP make GameManager have instances of these
    IGameState* dialogState = new DialogState;
    IGameStateView* dialogView = new DialogBoxView;
    dialogView->SetZOrder( ZTOP_MOST );
    gameManager->AddState( dialogState, dialogView );

    console->Print( "--------------- Starting Engine Script -----------------------" );
    g_kernel->GetConsole()->Exec( ("run " + cvars->GetStringValue( "startscript" ) ).c_str() ); 

}


// Engine entry point
int _tmain(int argc, _TCHAR* argv[])
{
    g_kernel = new Kernel();
    // Init engine
    if ( !g_kernel->Init() )
    {
        /**
         * Init all engine subsystems
         */
        SetupEngine();
       
        // Run processes
        g_kernel->Execute();
    }
    // savely shutdown
    g_kernel->Shutdown();

    // delete the engine
    if ( g_kernel )
        delete g_kernel;
    return 0;

}

